package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryBrandService categoryBrandService;


    /**
     * 根据品牌id集合查询品牌集合
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids")List<Long> ids) {
        List<BrandDTO> brandList = brandService.queryBrandByIds(ids);

        return ResponseEntity.ok(brandList);
    }

    /**
     * 根据id查询品牌
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandByBid(@PathVariable("id") Long bid) {
        BrandDTO brandDTO = brandService.queryBrandByBid(bid);
        return ResponseEntity.ok(brandDTO);
    }

    /**
     * 分页查询品牌
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> queryPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key
            ){
        return ResponseEntity.ok(brandService.queryPage(page, rows, key));
    }

    /**
     * 根据分类id查询品牌
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCid(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(categoryBrandService.queryBrandByCid(cid));
    }

    /**
     * 新增品牌
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO) {
        brandService.addBrand(brandDTO);
        return ResponseEntity.ok().build();

    }

    /**
     * 更新品牌
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除品牌
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id") Long bid) {
        brandService.deleteByBid(bid);
        return ResponseEntity.ok().build();
    }

//    @PostMapping("/saveImage")
//    public ResponseEntity<String> saveImage(@RequestParam("filename") String filename) {
//        System.out.println("filename = " + filename);
//        redisTemplate.opsForSet().add(RedisConstant.BRAND_IMAGE, "http://project2.oss-cn-shanghai.aliyuncs.com/"+filename);
//        return ResponseEntity.ok("ok");
//    }

//    @PostMapping("/saveImage")
//    public ResponseEntity<String> saveImage(@RequestParam("imgUrl") String filename) {
////        JSONObject jsonObject = new JSONObject();
////        jsonObject.put("filename",filename);
////        String jsonString = jsonObject.toString();
////        String filename = String.valueOf(map.get("image"));
//        System.out.println("filename = " + filename);
////        redisTemplate.opsForSet().add(RedisConstant.BRAND_IMAGE, StringUtils.substringAfter(filename,"heima01/"));
//        redisTemplate.opsForSet().add(RedisConstant.BRAND_IMAGE, filename);
////        return ResponseEntity.ok(jsonString);
//        return ResponseEntity.ok("ok");
//    }
}
